using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
//using Microsoft.Office.Server.Search.Query;
using SharePointSearchBench.Library;

namespace SharePointSearchBench
{
    /// <summary>
    /// Represents main window data that can be persisted or loaded.
    /// </summary>
    public class AppSessionState
    {
        string serviceUrl;
        string userName;
        string password;
        string domain;
        string fullTextQueryText;
        string keywordQueryText;
        string keywordViewFieldsText;
        string keywordSortFieldsText;
        bool useObjectModel;
        int resultCount;
        SearchType queryType;
        int startAt;
        SearchResultType searchResultType;

        /// <summary>
        /// Gets or sets the web service or site Url.
        /// </summary>
        public string ServiceUrl
        {
            get { return this.serviceUrl; }
            set { this.serviceUrl = value; }
        }

        /// <summary>
        /// Gets or sets the web service username.
        /// </summary>
        public string UserName
        {
            get { return this.userName; }
            set { this.userName = value; }
        }

        //TODO: password should be encrypted.

        /// <summary>
        /// Gets or sets the web service password.
        /// </summary>
        public string Password
        {
            get { return this.password; }
            set { this.password = value; }
        }

        /// <summary>
        /// Gets or sets the web service domain.
        /// </summary>
        public string Domain
        {
            get { return this.domain; }
            set { this.domain = value; }
        }

        /// <summary>
        /// Gets or sets the query type used in the window.
        /// </summary>
        public SearchType QueryType
        {
            get { return this.queryType; }
            set { this.queryType = value; }
        }


        public SearchResultType SearchResultType
        {
            get { return this.searchResultType; }
            set { this.searchResultType = value; }
        }

        /// <summary>
        /// Gets or sets whether the object model or web service option is selected.
        /// </summary>
        public bool UseObjectModel
        {
            get { return this.useObjectModel; }
            set { this.useObjectModel = value; }
        }

        /// <summary>
        /// Gets or sets the value for the result count text box.
        /// </summary>
        public int ResultCount
        {
            get { return this.resultCount; }
            set { this.resultCount = value; }
        }

        public int StartAt
        {
            get { return this.startAt; }
            set { this.startAt = value; }
        }

        /// <summary>
        /// Gets or sets the full text SQL query.
        /// </summary>
        public string FullTextQueryText
        {
            get { return this.fullTextQueryText; }
            set { this.fullTextQueryText = value; }
        }

        /// <summary>
        /// Gets or sets the keyword query phrase.
        /// </summary>
        public string KeywordQueryText
        {
            get { return this.keywordQueryText; }
            set { this.keywordQueryText = value; }
        }

        /// <summary>
        /// Gets or sets the list of fields used for a keyword query.
        /// </summary>
        public string KeywordViewFieldsText
        {
            get { return this.keywordViewFieldsText; }
            set { this.keywordViewFieldsText = value; }
        }

        /// <summary>
        /// Gets or sets the list of sorting fields used for a keyword query.
        /// </summary>
        public string KeywordSortFieldsText
        {
            get { return this.keywordSortFieldsText; }
            set { this.keywordSortFieldsText = value; }
        }

        /// <summary>
        /// Takes a main window object and persists its data to disk.
        /// </summary>
        /// <param name="window"></param>
        /// <param name="path"></param>
        public static void SaveState(MainWindow window, string path)
        {
            AppSessionState state = new AppSessionState();
            state.FullTextQueryText = window.queryTextBox.Text;
            state.KeywordQueryText = window.keywordPhraseTextBox.Text;
            state.KeywordViewFieldsText = window.keywordFieldsTextBox.Text;
            state.ServiceUrl = window.webServiceUrlComboBox.Text;
            state.Domain = window.domainComboBox.Text;
            state.Password = window.passwordTextBox.Password;
            state.UserName = window.usernameComboBox.Text;
            state.UseObjectModel = window.objectModelButton.IsChecked.Equals(true);
            state.KeywordSortFieldsText = window.keywordSortFieldsTextBox.Text;
            state.StartAt = int.Parse(window.startAtTextBox.Text);
            state.ResultCount = int.Parse(window.numberOfResultsTextBox.Text);

            if(window.highConfidenceResultsButton.IsChecked.Equals(true))
            {
                state.SearchResultType = SearchResultType.HighConfidence;
            }

            if(window.specialTermsResultsButton.IsChecked.Equals(true))
            {
                state.SearchResultType = SearchResultType.SpecialTerm;
            }

            if(window.relevantResultsButton.IsChecked.Equals(true))
            {
                state.SearchResultType = SearchResultType.Relevant;
            }
            
            if(window.fullTextButton.IsChecked.Equals(true))
            {
                state.QueryType = SearchType.FullText;
            }
            else if(window.keywordButton.IsChecked.Equals(true))
            {
                state.QueryType = SearchType.Keyword;
            }

            FileStream stream = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(AppSessionState));
                stream = new FileStream(path, FileMode.Create, FileAccess.Write);
                serializer.Serialize(stream, state);
            }
            finally
            {
                if(stream != null)
                {
                    stream.Dispose();
                }
            }
        }

        /// <summary>
        /// Loads up a main window object from a file that contains its persisted state.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="window"></param>
        public static void LoadFromState(string path, MainWindow window)
        {
            FileStream stream = null;
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(AppSessionState));
                stream = new FileStream(path, FileMode.Open);
                AppSessionState state = serializer.Deserialize(stream) as AppSessionState;

                AppSessionState.LoadFromState(state, window);
            }
            finally
            {
                if(stream != null)
                {
                    stream.Dispose();
                }
            }
        }

        /// <summary>
        /// Loads up a main window object from a persisted state object.
        /// </summary>
        /// <param name="state"></param>
        /// <param name="window"></param>
        public static void LoadFromState(AppSessionState state, MainWindow window){
            window.queryTextBox.Text = state.FullTextQueryText;
            window.keywordPhraseTextBox.Text = state.KeywordQueryText;
            window.keywordFieldsTextBox.Text = state.KeywordViewFieldsText;
            window.webServiceUrlComboBox.Text = state.ServiceUrl;
            window.domainComboBox.Text = state.Domain;
            window.passwordTextBox.Password = state.Password;
            window.usernameComboBox.Text = state.UserName;
            window.keywordSortFieldsTextBox.Text = state.KeywordSortFieldsText;
            window.startAtTextBox.Text = state.StartAt.ToString();
            window.numberOfResultsTextBox.Text = state.ResultCount.ToString();

            if (state.SearchResultType == SearchResultType.SpecialTerm)
            {
                window.highConfidenceResultsButton.IsChecked = false;
                window.specialTermsResultsButton.IsChecked = true;
                window.relevantResultsButton.IsChecked = false;
            }
            else if (state.SearchResultType == SearchResultType.HighConfidence)
            {
                window.highConfidenceResultsButton.IsChecked = true;
                window.specialTermsResultsButton.IsChecked = false;
                window.relevantResultsButton.IsChecked = false;
            }
            else if (state.SearchResultType == SearchResultType.Relevant)
            {
                window.highConfidenceResultsButton.IsChecked = false;
                window.specialTermsResultsButton.IsChecked = false;
                window.relevantResultsButton.IsChecked = true;
            }

            window.webServiceConnectionPanel.Visibility = System.Windows.Visibility.Visible;

            if (state.UseObjectModel)
            {
                window.objectModelButton.IsChecked = true;
                window.webServiceButton.IsChecked = false;
            }
            else
            {
                window.objectModelButton.IsChecked = false;
                window.webServiceButton.IsChecked = true;
            }

            if (state.QueryType == SearchType.FullText)
            {
                window.keywordButton.IsChecked = false;
                window.fullTextButton.IsChecked = true;
                window.fullTextBorder.Visibility = System.Windows.Visibility.Visible;
                window.keywordFieldsBorder.Visibility = System.Windows.Visibility.Collapsed;
                window.keywordPhraseBorder.Visibility = System.Windows.Visibility.Collapsed;

            }
            else
            {
                window.keywordButton.IsChecked = true;
                window.fullTextButton.IsChecked = false;
                window.fullTextBorder.Visibility = System.Windows.Visibility.Collapsed;
                window.keywordFieldsBorder.Visibility = System.Windows.Visibility.Visible;
                window.keywordPhraseBorder.Visibility = System.Windows.Visibility.Visible;
            }
        }

    }
}
